package com.iotechn.unimall.admin.api.print;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.gson.Gson;
import com.iotechn.unimall.admin.util.HttpClientUtil;
import com.iotechn.unimall.admin.util.SignUtils;
import com.iotechn.unimall.admin.util.SunmiConfig;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.PrintDO;
import com.iotechn.unimall.data.mapper.PrintMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class PrintServiceImpl implements PrintService {
    @Autowired
    private PrintMapper printMapper;

     @Override
        public Page<PrintDO> list(Integer page, Integer limit, Long adminId) throws ServiceException {
            Wrapper<PrintDO> wrapper = new EntityWrapper<PrintDO>();
            /**if (!StringUtils.isEmpty(name)) {
                wrapper.like("username", name);
            }**/
            wrapper.orderBy("id", false);
            Integer count = printMapper.selectCount(wrapper);
            List<PrintDO> PrintDOS = printMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
            return new Page<>(PrintDOS, page, limit, count);
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public PrintDO create(PrintDO printDO, Long adminId) throws ServiceException {
            printDO.setAdminId(adminId);
            Date now=new Date();
            printDO.setGmtCreate(now);
            printDO.setGmtUpdate(now);
            printDO.setStatus(0);
            if (printMapper.insert(printDO) > 0) {
                printDO.setId(printDO.getId());
                return printDO;
            }
             throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public String update(PrintDO printDO, Long adminId) throws ServiceException {
            Long id = printDO.getId();
            if (id == null) {
               return "fail";
            }
            Date now=new Date();
            printDO.setGmtUpdate(now);
            if (printMapper.updateById(printDO) > 0) {
                return "ok";
            }
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
     }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (printMapper.deleteById(id) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);

     }

    @Override
    public PrintDO detail(Long id, Long adminId) throws ServiceException {
       return printMapper.selectById(id);
    }

    @Override
        public Boolean updateStatus(Long adminId, Long id,Integer status) throws Exception {
            if(status == null || (status != 0 && status != 1)){
                throw new AdminServiceException(ExceptionDefinition.USER_INFORMATION_MISSING);
            }
            PrintDO printDO = new PrintDO();
            printDO.setId(id);
            printDO.setStatus(status);
            printDO.setGmtUpdate(new Date());
            if(printMapper.updateById(printDO) > 0){
                HttpClientUtil httpClientUtil=new HttpClientUtil();
                if(status!=null && status==1){
                    //绑定

                    String url= SunmiConfig.RESURL+"/v1/printer/printerAdd";
                    Map<String,Object> map=new HashMap<String,Object>();
                    SignUtils su=new SignUtils();
                    String timestamp=su.getTimestamp();
                    map.put("app_id",printDO.getAppId());
                    map.put("msn",SunmiConfig.msn);
                    map.put("timestamp",timestamp);
                    map.put("shop_id",printDO.getStoreId());
                    String sign=su.createSign(map,printDO.getAppKey());
                    map.put("sign",sign);
                    String ss=httpClientUtil.doPost(url, map);
                    System.out.println("printerAdd===>"+ss);
                    Gson gson=new Gson();
                    Map mapReturn=gson.fromJson(ss,Map.class);
                    if(mapReturn.get("code").equals("10000")){
                        System.out.println("mapReturn===>"+ss);

                    }
                }else{
                    //解绑
                    String url= SunmiConfig.RESURL+"/v1/printer/printerUnBind";
                    Map<String,Object> map=new HashMap<String,Object>();
                    SignUtils su=new SignUtils();
                    String timestamp=su.getTimestamp();
                    map.put("app_id",printDO.getAppId());
                    map.put("msn",SunmiConfig.msn);
                    map.put("timestamp",timestamp);
                    map.put("shop_id",printDO.getStoreId());
                    String sign=su.createSign(map,printDO.getAppKey());
                    map.put("sign",sign);
                    String ss=httpClientUtil.doPost(url, map);
                    Gson gson=new Gson();
                    Map mapReturn=gson.fromJson(ss,Map.class);
                    if(mapReturn.get("code").equals("10000")){
                        System.out.println("mapReturn===>"+ss);

                    }
                    System.out.println("printerUnBind===>"+ss);
                }

                return true;
            }
            return false;
        }

}
